package cn.lazydong.dbproperties.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

/**
 * @author HeLingdong
 */
public class DbPropertySourcesEnvironment implements EnvironmentPostProcessor, Ordered {
    public static String db_PropertySourcesName = "db-PropertySources";
    public static final Properties PROPERTIES = new Properties();
    public static MutablePropertySources propertySources;

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        propertySources = environment.getPropertySources();
        if (!environment.getPropertySources().contains(db_PropertySourcesName)) {
            environment.getPropertySources().addLast(new PropertiesPropertySource(db_PropertySourcesName, PROPERTIES));
        }
    }

    @Override
    public int getOrder() {
        // 这里将优先级设置为最高，方便其他的 EnvironmentPostProcessor 接口中也可以使用 DbPropertiesUtil工具类
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
